package com.lance.answer.question_answer.xj_new;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.CourseInfo;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author fengx
 * 新疆一体化答题-考试    ----------迪尼达·马合木提
 *
 * </p>
 */

@Slf4j
public class BatchDoXJAnswerTestingXingshiyuzhengce {




    private static List<WebElement> findWeElements(WebDriver driver, String cssSelector, int second) {
        return (new WebDriverWait(driver, Duration.ofSeconds(second))).until((ExpectedCondition<List<WebElement>>) d -> {
            assert d != null;
            return d.findElements(By.cssSelector(cssSelector));
        });
    }


    public static void main(String[] args) throws IOException {

        List<String> vip = Lists.newArrayList(
                "654022199006184837",
                "652324199902180014",
                "65020319940820072X",
                "654027199708040012",
                "650104200007073332",
                "652923199912111115",
                "652301197708086012",
                "652925199808240032",
                "653222199803270041",
                "652901199612185034",
                "622101197401030317",
                "650121199706152428",
                "652323199605190017",
                "622628199306187021",
                "652928198912211826",
                "622323198809137429",
                "622323199005037443",
                "653024199808111412",
                "652201199809073513",
                "652301199301217171",
                "510921200003031230",
                "654222197912283425",
                "620523199911085315",
                "622822199902090317",
                "622323199611131464",
                "65230119900602553X",
                "620525199807133615",
                "342422198901195818",
                "654223199803152413",
                "650121199611162447",
                "650108199107061929",
                "622428199406051913",
                "320623199605100024",
                "659001199810024829",
                "65412719800219271X",
                "652801199803046111",
                "654128199201231524",
                "654125199008102641",
                "653130198106123177",
                "65230120010101202X",
                "654028200303101827",
                "650103198011121329",
                "652123199906302528",
                "51072219950112742X",
                "421087199002227353",
                "411426199910100355",
                "620525200108263416",
                "654101200205180037",
                "652201198702223513",
                "610122197701200638",
                "620502198204055890",
                "412928197508010310"
        );

        // 校验是否提交
        int sync = 2 ;


        //预加载所有用户信息
        InputStream inputStream3 = new FileInputStream("E:\\alluser.txt");
        List<String> allUsers = IOUtils.readLines(inputStream3, "utf-8");


        LinkedBlockingDeque<CourseInfo> userCourseQueue = new LinkedBlockingDeque<>();
        for (String user : allUsers) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];
            if (pwd.length() > 14) {
                if (pwd.contains("X")) {
                    pwd = userCode.substring(11, 17);
                } else {
                    pwd = userCode.substring(12);
                }
            }
            if(!vip.contains(userCode)){
                continue;
            }


            int openCourseId  = 39679;
            String subjectName = "形式与政策（补考）";
            InputStream inputStream1 = new FileInputStream("D:\\新疆一体化测验作业\\" + openCourseId + "\\" + subjectName + "（测验作业）.json");
            String answerStr = IOUtils.toString(inputStream1, StandardCharsets.UTF_8);
            List<AnswerObject> listAnswerObject = JSONObject.parseArray(answerStr, AnswerObject.class);
            Map<String, AnswerObject> answerMap = Maps.newHashMap();
            for (AnswerObject answerObject : listAnswerObject) {
                answerMap.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
            }

            CourseInfo courseInfo = new CourseInfo();
            courseInfo.setAnswerMap(answerMap);
            courseInfo.setOpenCourseId(openCourseId);
            courseInfo.setSubjectName(subjectName);
            courseInfo.setPwd(pwd.toUpperCase(Locale.ROOT));
            courseInfo.setUser(userCode);
            userCourseQueue.addLast(courseInfo);


        }







        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new BatchDoXJAnswerTestingTaskxingshiyuzhengce("d:\\ynouAutoStudy\\chromedriver.exe",userCourseQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }



    }







}
